package edu.ccut.saturn.studio.logic;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Document;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.OneCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnEndNode;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnInformationNodeInAction;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnStartNodeInAction;
import edu.ccut.saturn.studio.logic.model.xml.loader.InformationNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.logic.StartNodeInLogicLoader;
import edu.ccut.saturn.studio.util.ConversionUtil;

public class ActionWizardPage extends SaturnWizardPage {

	private Text textware;
	private Text textdec;
	private Text textkey;
	private Text textname;
	private Text textfilename;
	private Shape shape = null;

	private static final String LABLE_FILENAME = Messages
			.getString("ActionWizardPage.filename"); //$NON-NLS-1$
	private static final String LABLE_NAME = Messages
			.getString("ActionWizardPage.name"); //$NON-NLS-1$
	private static final String LABEL_KEY = Messages
			.getString("ActionWizardPage.keyvalue"); //$NON-NLS-1$
//	private static final String LABEL_WARE = Messages
//			.getString("ActionWizardPage.component"); //$NON-NLS-1$
	private static final String LABEL_DEC = Messages
			.getString("ActionWizardPage.description"); //$NON-NLS-1$
//	private static final String SEARCH_IMAGE_SMALL = "icons/amplify_minus.jpg";
//	private static final String SEARCH_IMAGE_BIG = "icons/amplify_plus.jpg";
	public static final String FILE_TYPE = ".action"; 
	private static final String START_NODE_VALUE = Messages
			.getString("ActionWizardPage.start"); //$NON-NLS-1$
	private static final String END_NODE_VALUE = Messages
			.getString("ActionWizardPage.end"); //$NON-NLS-1$
//	private static final String TITLE_NAME = Messages
//			.getString("ActionWizardPage.ActionComponent"); //$NON-NLS-1$

	public static final String ACTION_WIZARD_FILE_NAME = "ACTION_FILE_NAME"; 
	private static final String TEMPLATE_SATURNINFO_NAME = "saturnInfo"; 
	private static final String TEMPLATE_SATURNSTART_NAME = "saturnStart"; 
	private static final String TEMPLATE_SATURNEND_NAME = "saturnEnd"; 
	private static final String TEMPLATE_SATURNCOMPONENT_NAME = "saturnComponent"; 

	public ActionWizardPage() {
		super();
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		setControl(container);
		setTitle(Messages.getString("ActionWizardPage.ActionComponent1")); //$NON-NLS-1$

		final Label label = new Label(container, SWT.NONE);
		label.setText(LABLE_FILENAME);

		textfilename = new Text(container, SWT.BORDER);
		textfilename.setTextLimit(50);
		textfilename.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
				textkey.setText(getComponentKey(textfilename.getText().trim()) + ".action" ); 
			}
		});
		final GridData gd_textfilename = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		textfilename.setLayoutData(gd_textfilename);
		new Label(container, SWT.NONE);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText(LABLE_NAME);

		textname = new Text(container, SWT.BORDER);
		textname.setTextLimit(50);
		textname.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_textname = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textname.setLayoutData(gd_textname);
		new Label(container, SWT.NONE);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setText(LABEL_KEY);

		textkey = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		textkey.setTextLimit(150);
		final GridData gd_textkey = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textkey.setLayoutData(gd_textkey);
		new Label(container, SWT.NONE);

		final Label label_4 = new Label(container, SWT.NONE);
		label_4.setLayoutData(new GridData());
		label_4.setText(Messages.getString("ActionWizardPage.Component")); //$NON-NLS-1$

		textware = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_textware = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textware.setLayoutData(gd_textware);

		final Label label_5 = new Label(container, SWT.NONE);
		label_5.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				label_5.setImage(
//						SWTResourceManager.getImage(
//						ActionWizardPage.class, SEARCH_IMAGE_SMALL));
						Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				label_5.setImage(
//						SWTResourceManager.getImage(
//						ActionWizardPage.class, SEARCH_IMAGE_BIG));
						Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		label_5.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				OneCompontentSearchDialog searchDialog = new OneCompontentSearchDialog(Display
						.getDefault().getShells()[0],"logic",SaturnProjectUtil.getProject(getSelection()));	
				
					if (searchDialog.open() == Window.OK) {
						SearchResult expression = (SearchResult)searchDialog.getFirstResult();
						textware.setText(expression.getKey());
						Document doc = expression.getDocument();
						
						StartNodeInLogicLoader componentLoader = new StartNodeInLogicLoader();
						InformationNodeLoader infoLoader = new InformationNodeLoader();
						List<Shape> components = componentLoader.loadNodes(doc
								.getDocumentElement());
						List<Shape> infos = infoLoader.loadNodes(doc
								.getDocumentElement());
						if (components != null && components.size() != 0) {
							shape = components.get(0);
							shape.setName(infos.get(0).getName());
							shape.setDescription(infos.get(0).getDescription());
							shape.setKey(expression.getKey());
							ConversionUtil.scopeMap(shape.getParameters());
						}
					}
					
				}
			});
		label_5.setLayoutData(new GridData());
		label_5.setImage(Activator.getImage(Images.AMPLIFY_PLUS));

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setText(LABEL_DEC);

		textdec = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		textdec.setTextLimit(200);
		final GridData gd_textdec = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_textdec.heightHint = 117;
		textdec.setLayoutData(gd_textdec);
		new Label(container, SWT.NONE);

		validate();

	}

	private SaturnInformationNodeInAction iniInfoNode() {
		SaturnInformationNodeInAction infoNode = new SaturnInformationNodeInAction();
		infoNode.setDescription(textdec.getText().trim());
		infoNode.setKey(textkey.getText());
		infoNode.setName(textname.getText().trim());
		return infoNode;
	}

	private SaturnStartNodeInAction iniStartNode() {
		SaturnStartNodeInAction startNode = new SaturnStartNodeInAction();
		startNode.setName(START_NODE_VALUE);
		return startNode;
	}

	private SaturnEndNode iniEndNode() {
		SaturnEndNode endNode = new SaturnEndNode();
		endNode.setName(END_NODE_VALUE);
		return endNode;
	}

	@Override
	public void initalValues(Map<String, Object> values) {
		SaturnInformationNodeInAction info = iniInfoNode();
		SaturnStartNodeInAction start = iniStartNode();
		SaturnEndNode end = iniEndNode();

		values.put(ACTION_WIZARD_FILE_NAME, this.textfilename.getText().trim());

		values.put(TEMPLATE_SATURNINFO_NAME, info);
		values.put(TEMPLATE_SATURNSTART_NAME, start);
		values.put(TEMPLATE_SATURNEND_NAME, end);
		values.put(TEMPLATE_SATURNCOMPONENT_NAME, this.shape);
	}

	// 输入值校验方法
	public boolean validate() {
		String fileName = textfilename.getText().trim();
		String actionName = textname.getText().trim();
		String description = textdec.getText().trim();

		setPageComplete(false);

		if (new File(getFullPath() + "/" + fileName + ".action").exists()) {
			setErrorMessage(Messages.getString("ActionWizardPage.FilenameExist")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(fileName)) {
			setErrorMessage(Messages.getString("ActionWizardPage.FilenameLimit")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(fileName)) {
			setErrorMessage(Messages.getString("ActionWizardPage.FilenameLimit1")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(actionName)) {
			setErrorMessage(Messages.getString("ActionWizardPage.namelimit")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(actionName)) {
			setErrorMessage(Messages.getString("ActionWizardPage.namelimit1")); //$NON-NLS-1$
			return false;
		}
		
		if (SaturnValidation.isEmpty(description)) {
			setDescription(Messages.getString("ActionWizardPage.InformationEmpty"));  //$NON-NLS-1$
		}
		
		setErrorMessage(null);
		setPageComplete(true);
		return true;

	}
}
